package com.xiaobai.netease_news.fragments;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.PopupWindow;

import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.activities.HeadLineActivity;
import com.xiaobai.netease_news.adapters.CommonGridViewAdapter;
import com.xiaobai.netease_news.adapters.MyPagerAdapter;
import com.xiaobai.netease_news.activities.TopNewsActivity;
import com.xiaobai.netease_news.constants.Constant;
import com.xiaobai.netease_news.views.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment
        implements View.OnClickListener, AdapterView.OnItemClickListener {

    private final String[] TITLES = new String[]{"头条", "娱乐", "科技", "手机", "原创",
            "游戏", "军事", "历史", "体育"};
    private PopupWindow popupWindow;

    private final int[] IMG_IDS = new int[]{R.drawable.more_menu_search_selector,
            R.drawable.more_menu_headline_selector, R.drawable.more_menu_offline_selector,
            R.drawable.more_menu_theme_selector, R.drawable.more_menu_scan_selector,
            R.drawable.more_menu_invitation_selector};
    private final int[] IMG_DESC = new int[]{R.string.more_menu_search,
            R.string.more_menu_headline, R.string.more_menu_offline, R.string.more_menu_theme,
            R.string.more_menu_scan, R.string.more_menu_invitation};
    private GridView weatherGridView;

    public NewsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout weatherWindow = (LinearLayout) LayoutInflater.from(getActivity())
                .inflate(R.layout.weather_window_layout, null);
        popupWindow = new PopupWindow(weatherWindow, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, true);

        weatherGridView = (GridView) weatherWindow.findViewById(R.id.weather_grid_view);
        List<Map<String, Integer>> icons = new ArrayList<>();
        for (int i = 0; i < IMG_IDS.length; i++) {
            Map<String, Integer> map = new HashMap<>();
            map.put("icon", IMG_IDS[i]);
            map.put("desc", IMG_DESC[i]);
            icons.add(map);
        }
        CommonGridViewAdapter adapter = new CommonGridViewAdapter(getActivity(), icons);
        weatherGridView.setAdapter(adapter);
        weatherGridView.setOnItemClickListener(this);

        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news, container, false);

        ImageView toTopNews = (ImageView) view.findViewById(R.id.to_top_news);
        toTopNews.setOnClickListener(this);

        List<Fragment> fragments = new ArrayList<>();

        NewsListFragment newsListFragment = new NewsListFragment();
        Bundle args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_TOPIC);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_ENTERTAINMENT);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_TECH);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_PHONE);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_ORIGINAL);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_GAME);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_MILITARY);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_HISTORY);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        newsListFragment = new NewsListFragment();
        args = new Bundle();
        args.putString(Constant.ARG_CONTENT_LIST_TYPE, Constant.NEWS_SPORT);
        newsListFragment.setArguments(args);
        fragments.add(newsListFragment);

        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), fragments, TITLES);

        ViewPager newsPager = (ViewPager) view.findViewById(R.id.news_pager);
        newsPager.setAdapter(adapter);

        PagerSlidingTabStrip pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.news_pager_sliding_tab);
        pagerSlidingTabStrip.setViewPager(newsPager);

        ImageView toWeather = (ImageView) view.findViewById(R.id.to_weather);
        toWeather.setOnClickListener(this);

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.to_top_news:
                startActivity(new Intent(getActivity(), TopNewsActivity.class));
                break;
            case R.id.to_weather:
                showPopupWindow(v);
                break;
        }
    }

    private void showPopupWindow(View v) {
        if (popupWindow.isShowing())
            popupWindow.dismiss();
        else {
            LayoutAnimationController controller =
                    new LayoutAnimationController(AnimationUtils.loadAnimation(getActivity(),
                            R.anim.anim_grid_view_zoom_in));
            controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
            weatherGridView.setLayoutAnimation(controller);
            weatherGridView.startLayoutAnimation();
            popupWindow.showAsDropDown(v);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                break;
            case 1:
                startActivity(new Intent(getActivity(), HeadLineActivity.class));
                popupWindow.dismiss();
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
        }
    }
}
